package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itcast.donkey.R;
import com.itcast.donkey.application.MyApplication;
import com.itcast.donkey.modles.TranslateModles;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.Iterator;
import java.util.List;

/**
 * Created by lanouhn on 16/9/18.
 */
public class TranslateContentAdapter extends RecyclerView.Adapter<TranslateContentAdapter.TranslateViewHolder> {

    private Context context;
    private List<TranslateModles> modlesList;


    public TranslateContentAdapter(Context context, List<TranslateModles> modlesList) {
        this.context = context;
        this.modlesList = modlesList;
    }

    @Override
    public TranslateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_translate_content, null);
        TranslateViewHolder holder = new TranslateViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TranslateViewHolder holder, final int position) {

        holder.tvTranslateFront.setText(modlesList.get(position).getTranslateFront());
        holder.tvTranslateEnd.setText(modlesList.get(position).getTranslateEnd());

        holder.ivScale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 分享的弹出框
                 */
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View view = View.inflate(context, R.layout.dialog_scale_front, null);
                builder.setView(view);
                builder.setCancelable(false);
                final AlertDialog ad = builder.create();
                TextView btn_cancel = (TextView) view.findViewById(R.id.btn_translate_front);
                btn_cancel.setText(modlesList.get(position).getTranslateEnd());
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.cancel();
                    }
                });
                ad.show();

            }
        });

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "已删除", Toast.LENGTH_SHORT).show();
                MyApplication application = new MyApplication();
                DbManager db = x.getDb(application.getConfig());
                try {
                    TranslateModles modles = db.selector(TranslateModles.class).where("translateEnd", "=", modlesList.get(position).getTranslateEnd()).findFirst();
                    db.delete(modles);
                    Iterator<TranslateModles> iterator = modlesList.iterator();
                    while(iterator.hasNext()){
                        TranslateModles modles1 = iterator.next();
                        int o_id = modles1.getId();
                        int n_id = modles.getId();
                        if (o_id == n_id) {
                            iterator.remove();
                        }
                    }
                    notifyDataSetChanged();
                } catch (DbException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return modlesList.size();
    }

    public class TranslateViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTranslateFront;
        private TextView tvTranslateEnd;
        private ImageView ivScale;
        private ImageView ivDelete;

        public TranslateViewHolder(View itemView) {
            super(itemView);
            tvTranslateFront = (TextView) itemView.findViewById(R.id.tv_translate_front);
            tvTranslateEnd = (TextView) itemView.findViewById(R.id.tv_translate_end);
            ivScale = (ImageView) itemView.findViewById(R.id.iv_scale);
            ivDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
        }
    }
}
